﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using LGE;

namespace MagicFight
{
    public class MagicSelectionScreen : GameScreen
    {
        Texture2D logoProtection;
        Texture2D logoHoly;
        Texture2D logoArcane;
        Texture2D logoElemental;

        Character character;
        int playerIndex;
        int x = 0;
        int y = 0;
        int nLines = 4;
        int nColumns = 8;
        Vector2 gradePosition = new Vector2(100, 100);
        Vector2 gradeSpacing = new Vector2(100, 100);
        Cursor cursor;

        public MagicSelectionScreen(Character character)
            : base()
        {
            this.character = character;
            this.playerIndex = character.playerIndex;
        }

        public override void LoadContent()
        {
            cursor = new Cursor();
            logoProtection = Engine.Load<Texture2D>("Images\\Logo\\protection50");
            logoHoly = Engine.Load<Texture2D>("Images\\Logo\\holy50");
            logoArcane = Engine.Load<Texture2D>("Images\\Logo\\arcane50");
            logoElemental = Engine.Load<Texture2D>("Images\\Logo\\elemental50");
        }

        public override void Update(GameTime gameTime)
        {
            if (Input.Pressed(playerIndex, KeyMap.Start))
                GameScreenManager.Pop();

            // ThumbStick
            if (Input.Pressed(playerIndex, KeyMap.Up)) y--;
            if (Input.Pressed(playerIndex, KeyMap.Down)) y++;
            if (Input.Pressed(playerIndex, KeyMap.Left)) x--;
            if (Input.Pressed(playerIndex, KeyMap.Right)) x++;
            x = (int)MathHelper.Clamp(x, 0, nColumns - 1);
            y = (int)MathHelper.Clamp(y, 0, nLines - 1);
            
            // Buttons
            for (int i = 0; i < 8; ++i) if (Input.Pressed(playerIndex, Def.ButtonIndex[i]))
                {
                    character.magic[i] = x + (y * nColumns);
                    Input.Vibrate(playerIndex, 150);
                }

            cursor.position = gradePosition + new Vector2(x * gradeSpacing.X, y * gradeSpacing.Y);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.GraphicsDevice.Clear(Color.Black);
            DrawGrade(gameTime, spriteBatch);
            cursor.Draw(spriteBatch);
        }

        private void DrawGrade(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Def.MagicDef m;
            for (int i = 0; i < nLines; ++i)
            {
                Texture2D texture = logoArcane;
                if (i == 1) texture = logoProtection;
                else if (i == 2) texture = logoHoly;
                else if (i == 3) texture = logoElemental;
                for (int j = 0; j < nColumns; ++j)
                {
                    m = Def.MagicDefs[j + (i * nColumns)];
                    spriteBatch.Draw(m.Logo, gradePosition + new Vector2(j * gradeSpacing.X, i * gradeSpacing.Y), Color.White);
                    spriteBatch.DrawString(MagicFight.Font, m.Name, new Vector2(j * gradeSpacing.X + 90, i * gradeSpacing.Y + 170), Color.White);
                }
            }

            // Footer
            Vector2 pos = gradePosition + new Vector2(0, nLines * gradeSpacing.Y);
            m = Def.MagicDefs[x + (y * nColumns)];
            String text = "Nome: " + m.Name;
            text += "\nTipo: " + m.Type.ToString() + "    Mana Cost: " + m.ManaCost + "    Point Cost: " + m.PointCost;
            text += "    Cast Time: " + (m.CastTime == 0 ? "instant" : ((double)m.CastTime / 1000).ToString("0.0") + "s");
            text += "\n" + WrapText("Descrition: " + m.Description, nColumns * gradeSpacing.Y);
            spriteBatch.DrawString(MagicFight.Font, text, pos, Color.White);
        }

        public string WrapText(string text, float maxLineWidth)
        {
            SpriteFont spriteFont = MagicFight.Font;
            string[] words = text.Split(' ');

            StringBuilder sb = new StringBuilder();

            float lineWidth = 0f;

            float spaceWidth = spriteFont.MeasureString(" ").X;

            foreach (string word in words)
            {
                Vector2 size = spriteFont.MeasureString(word);

                if (lineWidth + size.X < maxLineWidth)
                {
                    sb.Append(word + " ");
                    lineWidth += size.X + spaceWidth;
                }
                else
                {
                    sb.Append("\n" + word + " ");
                    lineWidth = size.X + spaceWidth;
                }
            }

            return sb.ToString();
        }
    }
}   
